/**   
 *    
 * 项目名称：SmartKungFuService   
 * 类名称：ClientRefundServlet   
 * 类描述：   退款接口
 * 创建人：zhanghl   
 * 创建时间：2016年11月29日 上午10:22:09   
 * 修改人：zhanghl   
 * 修改时间：2016年11月29日 上午10:22:09   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.servlet;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.ClientRefundReqPara;
import com.lst.entity.response.ClientRefundResPara;
import com.lst.model.ClientRefund;
import com.lst.model.MstOrder;
import com.lst.model.OrderDetail;
import com.lst.service.ClientRefundService;
import com.lst.service.MstOrderService;
import com.lst.service.OrderDetailService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: ClientRefundServlet
 * @Description: 退款申请接口
 * @author zhanghl
 * @date 2016年11月29日 上午10:22:09
 * 
 */
public class ClientRefundServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private ClientRefundService clientRefundService = ctx
			.getBean(ClientRefundService.class);

	private MstOrderService mstOrderService = ctx
			.getBean(MstOrderService.class);

	private OrderDetailService orderDetailService = ctx
			.getBean(OrderDetailService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);

		ClientRefundResPara resPara = new ClientRefundResPara();

		try {
			ClientRefundReqPara reqPara = getReqPara(request);

			if (isCheckField(reqPara, resPara)) {
				Integer clientid = Integer.valueOf(reqPara.getUserid());
				Integer orderid = Integer.valueOf(reqPara.getOrderid());
				Integer causeid = Integer.valueOf(reqPara.getCauseid());
				BigDecimal refprice = new BigDecimal(reqPara.getRefprice());
				Integer refnum = Integer.valueOf(reqPara.getRefnum());
				String content = reqPara.getContent();
				Integer orderstatus = Integer.valueOf(reqPara.getOrderstatus());
				Integer odid = Integer.valueOf(reqPara.getOdid());

				MstOrder mo = new MstOrder();
				OrderDetail od = new OrderDetail();
				ClientRefund cr = new ClientRefund();

				Map<String , Object> crmap = new HashMap<String, Object>();

				PageBounds pageBounds = new PageBounds();

				if (CommCode.SYSTEM_ORDER_STATUS_PAY.equals(orderstatus)) {				

					mo.setIsdeleted(true);
					mo.setId(orderid);
					mo.setUpdatedate(startDate);
					mo.setUpdateuser(clientid);

					mstOrderService.updateByPrimaryKeySelective(mo);

					od.setIsdeleted(true);
					od.setId(odid);
					od.setUpdatedate(startDate);
					od.setUpdateuser(clientid);

					orderDetailService.updateByPrimaryKeySelective(od);

					resPara.setCode(CommCode.M_Y000000);
					resPara.setMessage(CommCode.M_Y000001);

				} else if (CommCode.SYSTEM_ORDER_STATUS_WAIT.equals(orderstatus)) {

					crmap.put("enabled", true);
					crmap.put("isDeleted", false);
					crmap.put("orderid", orderid);			

					List<ClientRefund> crf = clientRefundService.queryByList(crmap, pageBounds); 

					if(crf.isEmpty()){

						cr.setClientid(clientid);
						cr.setOrderid(orderid);
						cr.setOrderdetailid(odid);
						cr.setCauseid(causeid);
						cr.setRefstatus(CommCode.SYSTEM_ORDER_REFUND_APPLT);
						cr.setRefprice(refprice);
						cr.setRefnum(refnum);
						cr.setContent(content);
						cr.setCreatedate(startDate);
						cr.setCreateuser(clientid);
						cr.setEnabled(true);
						cr.setIsdeleted(false);

						clientRefundService.insert(cr);

						//退款数量
						od.setId(odid);
						od.setOrderstatus(CommCode.SYSTEM_ORDER_STATUS_PAYBACKING);
						od.setUpdatedate(startDate);
						od.setUpdateuser(clientid);

						orderDetailService.updateByPrimaryKeySelective(od);

						resPara.setCode(CommCode.M_Y000000);
						resPara.setMessage(CommCode.M_H000006);

					}else{
						resPara.setCode(CommCode.M_A000000);
						resPara.setMessage(CommCode.M_H000008);
					}

				} else {
					resPara.setCode(CommCode.M_A000000);
					resPara.setMessage(CommCode.M_H000007);
				}

				logger.info("doPost end run(s): "
						+ DateUtil.calLastedTime(startDate));
			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
					.excludeFieldsWithoutExposeAnnotation().serializeNulls()
					.create();
			String jsonResult = gson.toJson(resPara);
			logger.info("ClientRefundServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end run(s): "
					+ DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public ClientRefundReqPara getReqPara(HttpServletRequest request) {

		ClientRefundReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request,
					new ClientRefundReqPara());

			reqPara = (ClientRefundReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String orderid = request.getParameter("orderid");
			String causeid = request.getParameter("causeid");
			String refprice = request.getParameter("refprice");
			String refnum = request.getParameter("refnum");
			String content = request.getParameter("content");
			String orderstatus = request.getParameter("orderstatus");
			String odid = request.getParameter("odid");

			logger.info("reqPara userid: " + userid);
			logger.info("reqPara orderid: " + orderid);
			logger.info("reqPara causeid: " + causeid);
			logger.info("reqPara refprice: " + refprice);
			logger.info("reqPara refnum: " + refnum);
			logger.info("reqPara content: " + content);
			logger.info("reqPara orderstatus: " + orderstatus);
			logger.info("reqPara odid: " + odid);

			reqPara.setUserid(userid);
			reqPara.setOrderid(orderid);
			reqPara.setCauseid(causeid);
			reqPara.setRefprice(refprice);
			reqPara.setRefnum(refnum);
			reqPara.setContent(content);
			reqPara.setOrderstatus(orderstatus);
			reqPara.setOdid(odid);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		ClientRefundReqPara crReqPara = (ClientRefundReqPara) reqPara;
		ClientRefundResPara crResPara = (ClientRefundResPara) resPara;

		if (StringUtils.isEmpty(crReqPara.getUserid())) {
			crResPara.setCode(CommCode.M_A000000);
			crResPara.setMessage(CommCode.M_P000002);
		} else if (StringUtils.isEmpty(crReqPara.getOrderid())) {
			crResPara.setCode(CommCode.M_A000000);
			crResPara.setMessage(CommCode.M_E000009);
		} else if (StringUtils.isEmpty(crReqPara.getCauseid())) {
			crResPara.setCode(CommCode.M_A000000);
			crResPara.setMessage(CommCode.M_H000001);
		} else if (StringUtils.isEmpty(crReqPara.getOrderstatus())) {
			crResPara.setCode(CommCode.M_A000000);
			crResPara.setMessage(CommCode.M_H000005);
		} else if (StringUtils.isEmpty(crReqPara.getOdid())) {
			crResPara.setCode(CommCode.M_A000000);
			crResPara.setMessage(CommCode.M_E000017);
		} else {

			if (CommCode.SYSTEM_ORDER_STATUS_WAIT.equals(StringUtils
					.isEmpty(crReqPara.getOrderstatus()))) {
				if (StringUtils.isEmpty(crReqPara.getRefprice())) {
					crResPara.setCode(CommCode.M_A000000);
					crResPara.setMessage(CommCode.M_H000002);
				} else if (StringUtils.isEmpty(crReqPara.getRefnum())) {
					crResPara.setCode(CommCode.M_A000000);
					crResPara.setMessage(CommCode.M_H000003);
				} else if (StringUtils.isEmpty(crReqPara.getContent())) {
					crResPara.setCode(CommCode.M_A000000);
					crResPara.setMessage(CommCode.M_H000004);
				} else {
					flag = true;
				}
			} else {
				return true;
			}
		}
		return flag;
	}

}
